package com.dc.project.controller;

import com.dc.common.validated.InsertGroup;
import com.dc.common.validated.QueryPageGroup;
import com.dc.common.validated.UpdateGroup;
import com.dc.common.vo.ResultVo;
import com.dc.project.workflow.service.ActFlowService;
import com.dc.project.workflow.vo.ModelVo;
import com.dc.project.workflow.vo.ProcessModelVo;
import com.dc.project.workflow.vo.ProcessQueryVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhuangcy
 * @date 2021/6/30
 * @description 流程模型控制类
 */
@RestController
@RequestMapping("wfModel")
public class ActFlowModelController {
    @Autowired
    private ActFlowService actFlowService;

    /**
     * 分页查询流程设计模型表
     *
     * @param vo
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询流程设计模型表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("flow:wfModel:query")
    public ResultVo queryByPage(@Validated(QueryPageGroup.class) ProcessQueryVo vo) {
        return ResultVo.success().data(this.actFlowService.queryModelPage(vo));
    }

    /**
     * 创建模型
     *
     * @param vo
     */
    @PostMapping
    @ApiOperation("新增流程设计模型表")
    @RequiresPermissions("flow:wfModel:add")
    public ResultVo createModel(@Validated(InsertGroup.class) @RequestBody ModelVo vo) {
        actFlowService.createModel(vo);
        return ResultVo.success();
    }

    /**
     * 删除流程设计模型
     *
     * @param modelId
     * @return
     */
    @DeleteMapping("{modelId}")
    @ApiOperation("删除流程设计模型")
    @RequiresPermissions("flow:wfModel:delete")
    public ResultVo delete(@PathVariable String modelId) {
        actFlowService.deleteModel(modelId);
        return ResultVo.success();
    }

    /**
     * 发布流程
     *
     * @param dto
     * @return
     */
    @PutMapping("publish")
    @ApiOperation("发布流程")
    @RequiresPermissions("flow:wfModel:edit")
    public ResultVo publish(@Validated(UpdateGroup.class) @RequestBody ProcessModelVo dto) {
        actFlowService.publishModel(dto.getId());
        return ResultVo.success();
    }

    /**
     * 流程挂起与激活
     *
     * @param dto
     * @return
     */
    @PutMapping("suspendOrActivate")
    @ApiOperation("流程挂起与激活")
    @RequiresPermissions("flow:wfModel:edit")
    public ResultVo suspendOrActivate(@Validated(UpdateGroup.class) @RequestBody ProcessModelVo dto) {
        return ResultVo.success().data(actFlowService.suspendOrActivateProcessDefinition(dto.getId()));
    }

    /**
     * 获取流程实例任务图像
     *
     * @param key
     * @param category
     * @return
     */
    @ResponseBody
    @ApiOperation("获取流程实例任务图像")
    @GetMapping(value = "taskImage/{key}/{category}", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] getTaskImage(@PathVariable("key") String key, @PathVariable("category") String category) {
        return actFlowService.getTaskImage(key, category);
    }
}

